//
//  CartoonClassificationViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "CartoonClassificationViewController.h"
#import "Classification.h"
#import "ClassCollectionViewCell.h"
#import "RankViewController.h"
#import "UpdateViewController.h"
#import "ComicsViewController.h"
#import "ClassifyDetailViewController.h"
#import "MoreViewController.h"
@interface CartoonClassificationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) NSArray *titleArr;

@end

@implementation CartoonClassificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollectionView];
    [self handleData];
    
}

- (void)createCollectionView
{
    
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 375,100)];
    self.titleArr = @[@"排行",@"更新",@"条漫"];
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.tintColor = [UIColor whiteColor];
        btn.frame = CGRectMake(SelfViewWidth / 3 * i +5, 15, SelfViewWidth / 3 - 10, 30);
        btn.backgroundColor = [UIColor orangeColor];
        [btn setTitle:[_titleArr objectAtIndex:i] forState:UIControlStateNormal];
        btn.tag = i + 1000;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.headerView addSubview:btn];
    }
    [self.view addSubview:_headerView];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 130);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical ;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.minimumInteritemSpacing = 1;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 109, SelfViewWidth, SelfViewHeight - 64 - 49-45) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[ClassCollectionViewCell class] forCellWithReuseIdentifier:@"Classify_Cell"];
    
}

- (void)buttonClick:(UIButton *)btn
{
    
    if (btn.tag - 1000 == 0) {
      RankViewController *rank = [[RankViewController alloc] init];
        [self.navigationController pushViewController:rank animated:YES];
    }else if (btn.tag - 1000 == 1){
        
        UpdateViewController *updateVC = [[UpdateViewController alloc] init];
        [self.navigationController pushViewController:updateVC animated:YES];
        
    }else{
        
        ComicsViewController *comicsVC = [[ComicsViewController alloc] init];
        [self.navigationController pushViewController:comicsVC animated:YES];
    }
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    Classification *classify = [self.arr objectAtIndex:indexPath.item];
    ClassCollectionViewCell *cell = [ClassCollectionViewCell classifyCellWithCollectionView:collectionView cellForItemAtIndexPath:indexPath];
    cell.classifyModel = classify;
    return cell;
    
}

- (void)handleData
{
    
    [NetHandler dataWithUrl:@"http://mobilev3.ac.qq.com/Classify/comicClassifyListV2ForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4" complant:^(id data) {
        NSDictionary *dic = data;
        NSArray *dataArr = [dic objectForKey:@"data"];
        self.arr = [NSMutableArray array];
        self.arr = [Classification modelsWithArr:dataArr];
        [self.collectionView reloadData];
    }];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.item == self.arr.count - 1) {
        
        ComicsViewController *comicsVC = [[ComicsViewController alloc] init];
        [self.navigationController pushViewController:comicsVC animated:YES];
    }else{
        
        ClassifyDetailViewController *vc = [[ClassifyDetailViewController alloc] init];
        Classification *classify = [self.arr objectAtIndex:indexPath.item];
        vc.classify_id = classify.classify_id;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
